package tk.djcrazy.instantmessage.action;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.SessionAware;

import tk.djcrazy.instantmessage.service.IUserAvatarService;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAvatarAction extends ActionSupport implements SessionAware{
 	private static final long serialVersionUID = 7824829060200294675L;
 	private static final Log log = LogFactory.getLog(UploadAvatarAction.class);
 	@Resource
 	private IUserAvatarService userAvatarService;
 	
	private File avatar;
	private String avatarFileName;
	private String avatarPath;
	private Map<String, Object> session;
	
	public String execute() {
		log.debug("upload avatar action");
 		try {
 			avatarPath = "avatarfile/"+userAvatarService.storeAvatar(avatar, avatarFileName);
			session.put("avatar", avatarPath);
 			log.debug("upload avatar success");
		} catch (IOException e) {
			addActionError("服务器错误");
			log.error("upload avatar failed", e);
			e.printStackTrace();
			return ERROR;
		}
  		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		session = arg0;
	}

	public IUserAvatarService getUserAvatarService() {
		return userAvatarService;
	}

	public void setUserAvatarService(IUserAvatarService userAvatarService) {
		this.userAvatarService = userAvatarService;
	}

	public File getAvatar() {
		return avatar;
	}

	public void setAvatar(File avatar) {
		this.avatar = avatar;
	}

	public String getAvatarFileName() {
		return avatarFileName;
	}

	public void setAvatarFileName(String avatarFileName) {
		this.avatarFileName = avatarFileName;
	}

	public String getAvatarPath() {
		return avatarPath;
	}

	public void setAvatarPath(String avatarPath) {
		this.avatarPath = avatarPath;
	}
}
